package com.hupu.jrs.components.retrofit

import okhttp3.RequestBody
import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type
import java.nio.charset.Charset

/**
 * Created by sll on 2016/3/31.
 */
class FastJsonConverterFactory(private val charset: Charset) : Converter.Factory() {

    override fun requestBodyConverter(type: Type?,
                                      parameterAnnotations: Array<Annotation>?, methodAnnotations: Array<Annotation>?, retrofit: Retrofit?): Converter<*, RequestBody> {
        return FastJsonRequestBodyConverter<Type>(type!!, charset)
    }

    override fun responseBodyConverter(type: Type?, annotations: Array<Annotation>?,
                                       retrofit: Retrofit?): Converter<ResponseBody, *> {
        return FastJsonResponseBodyConverter<Type>(type!!, charset)
    }

    companion object {
        private val UTF_8 = Charset.forName("UTF-8")

        @JvmOverloads
        fun create(charset: Charset = UTF_8): FastJsonConverterFactory {
            return FastJsonConverterFactory(charset)
        }
    }
}
